package com.example.demo.controller;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("")
    public Object getAll(Integer pageNo, Integer pageSize) {
        if(pageNo==null) pageNo = 1;
        if(pageSize == null) pageSize = 1;
        PageHelper.startPage(pageNo, pageSize);
        List<User> list = userService.getAll();
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @GetMapping("/getBy")
    public Object getBy(String username, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<User> list = userService.getByName(username);
        PageInfo<User> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }
    @GetMapping("/getBy2")
    public Object getBy2(String username, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
//        List<User> list = userService.getByName(username);
//        PageInfo<User> pageInfo = new PageInfo<>(list);
        Page<User> pages = userService.getByName2(username);
        return pages;
    }
}
